﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data;
using DevExpress.Web.ASPxEditors;

using Entities;
using BLL;

namespace Stationery.UC
{
    public partial class UC_GroupNews : System.Web.UI.UserControl
    {
        BLL_News bll_News = new BLL_News();
        protected void Page_Load(object sender, EventArgs e)
        {
            int groupid = 0;
            groupid = Convert.ToInt32(Request.Params["gid"]);
            if (groupid == 0)
                return; //or InvalidPage();
                        
            //get data news by group_id
            ListNewsByGroup(groupid);            
        }

        public void ListNewsByGroup(int p_group_id)
        {
            DataSet ds = null;
            //int mid = 15;
            try
            {
                ds = bll_News.Get_NewsByGroupID(p_group_id);

                if (ds != null)
                {
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        DataView dv1 = new DataView(ds.Tables[0]);
                        DataListNews.DataSource = dv1;
                        DataListNews.DataBind();                        

                    }
                    else
                    {
                        DataListNews.DataSource = null;
                    }

                    if (ds.Tables[1].Rows.Count > 0)
                    {
                        DataView dv2 = new DataView(ds.Tables[1]);
                        DataListTitle.DataSource = dv2;
                        DataListTitle.DataBind();
                    }
                }                                    
            }
            catch (Exception ex)
            { 
            }
        }

        protected void DataListNews_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (e.Item != null)
            {
                DataRowView dtrv = (DataRowView)e.Item.DataItem;

                HyperLink hltitle = (HyperLink)e.Item.FindControl("HyperLinkTitle1");
                hltitle.Text = dtrv["Title"].ToString();
                hltitle.NavigateUrl = "~/News.aspx?viewtype=detail&gid=" + Request.Params["gid"].ToString() + "&newsid=" + dtrv["News_id"].ToString();
                
                Label label = (Label)e.Item.FindControl("LabelNewsDate");
                DateTime dt = (DateTime)dtrv["Create_date"];
                label.Text = "(" + dt.ToString("dd/MM/yyyy") + ")";
                
                ASPxBinaryImage binaryImage = (ASPxBinaryImage)e.Item.FindControl("ASPxBinaryImageData");
                if (dtrv["Image_data"] != DBNull.Value)
                {
                    binaryImage.ContentBytes = (byte[])dtrv["Image_data"];
                }
                else
                    binaryImage.ContentBytes = null;
                
                HtmlGenericControl hldesc = (HtmlGenericControl)e.Item.FindControl("DivDesc");
                hldesc.InnerHtml = dtrv["Content_desc"].ToString();

                HyperLink hlView = (HyperLink)e.Item.FindControl("HyperLinkView");
                hlView.Text = Utils.link_detail;
                hlView.NavigateUrl = "~/News.aspx?viewtype=detail&gid=" + Request.Params["gid"].ToString() + "&newsid=" + dtrv["News_id"].ToString();
            }
        }

        protected void DataListTitle_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (e.Item != null)
            {
                DataRowView dtrv = (DataRowView)e.Item.DataItem;

                HyperLink hltitle = (HyperLink)e.Item.FindControl("HyperLinkTitle2");
                hltitle.Text = dtrv["Title"].ToString();
                hltitle.NavigateUrl = "~/News.aspx?viewtype=detail&gid=" + Request.Params["gid"].ToString() + "&newsid=" + dtrv["News_id"].ToString();
                Label label = (Label)e.Item.FindControl("LabelDate");
                DateTime dt = (DateTime)dtrv["Create_date"];
                label.Text = "(" + dt.ToString("dd/MM") + ")";
            }
        }

        protected string GetLinkDetail(string group_id, string news_id)
        {
            string strURL = "";
            if (group_id == "")
                return strURL;
            if (news_id == "")
                return strURL;

            strURL = String.Format("News.aspx?viewtype=detail&gid={0}&newsid={1}", group_id, news_id);
            return strURL;
        }
    }
}